/*
 *  NPC.cpp
 *  SquirrelAdven
 *
 *  Created by Wen Xu on 3/7/11.
 *  Copyright 2011 __MyCompanyName__. All rights reserved.
 *
 */

#include "NPC.h"
#include "OBJtoTriMesh.h"
#include <iostream>

// constructor
NPC::NPC() {
	characterID = -1;
	characterMesh = NULL;
	dialogTree = NULL;
}

// destructor
NPC::~NPC() {
}

void NPC::computeBoundingBox() {
	float min_x, max_x, min_y, max_y, min_z, max_z;
	int i;
	int numVertices = characterMesh->GetNumVertices();
	
	// get the first vertice
	min_x = max_x = characterMesh->GetPosition(0)[0];
	min_y = max_y = characterMesh->GetPosition(0)[1];
	min_z = max_z = characterMesh->GetPosition(0)[2];
	
	for (i = 1; i < numVertices; i++) {
		Float3 v = characterMesh->GetPosition(i);
		
		// min x
		if (v[0] < min_x) {
			min_x = v[0];
		}
		
		// max x
		if (v[0] > max_x) {
			max_x = v[0];
		}
		
		// min y
		if (v[1] < min_y) {
			min_y = v[1];
		}
		
		// max y
		if (v[1] > max_y) {
			max_y = v[1];
		}
		
		// min z
		if (v[2] < min_z) {
			min_z = v[2];
		}
		
		// max z
		if (v[2] > max_z) {
			max_z = v[2];
		}
	}
	
	// compute length
	len_x = max_x - min_x;
	len_y = max_y - min_y;
	len_z = max_z - min_z;
	
	// compute center point
	//centerPoint[0] = (min_x + max_x) / 2;
	//centerPoint[1] = (min_y + max_y) / 2;
	//centerPoint[2] = (min_z + max_z) / 2;
	
	centerPoint[0] = 0;
	centerPoint[1] = 0;
	centerPoint[2] = 0;
	
	float half_len_x = len_x / 2;
	float half_len_z = len_z / 2;
	radius = half_len_x > half_len_z ? half_len_x :half_len_z;
}

// get TriMesh of the NPC
TriMesh * NPC::getCharacterMesh() {
	return characterMesh;
}

// get DialogTree
DialogTree * NPC::getDialogTree() {
	return dialogTree;
}

void NPC::setCharacterID(int ID) {
	characterID = ID;
}
void NPC::setCharacterTitle(std::string title) {
	characterTitle = title;
}

void NPC::setCharacterName(std::string name) {
	characterName = name;
}

void NPC::setCharacterSpecies(std::string species) {
	characterSpecies = species;
}

void NPC::setCharacterBackground(std::string background) {
	characterBackground = background;
}

void NPC::setObjFilename(std::string objFilename) {
	this->objFilename = objFilename;
}

void NPC::setDialogFilename(std::string dialogFilename) {
	this->dialogFilename = dialogFilename;
	dialogTree = new DialogTree(dialogFilename);
}

void NPC::setWmtfFilename(std::string wmtfFilename) {
	this->wmtfFilename = wmtfFilename;
	OBJtoTriMesh objToTriMesh;
	characterMesh = objToTriMesh.CreateMesh("./", objFilename, wmtfFilename);
	computeBoundingBox();
}

void NPC::setOriginalTranslate(float x, float y, float z) {
	t_x = x;
	t_y = y;
	t_z = z;
}

void NPC::setOriginalScale(float x, float y, float z) {
	s_x = x;
	s_y = y;
	s_z = z;
}

int NPC::getCharacterID() {
	return characterID;
}

std::string NPC::getCharacterTitle() {
	return characterTitle;
}


std::string NPC::getCharacterName() {
	return characterName;
}

std::string NPC::getCharacterSpecies() {
	return characterSpecies;
}

std::string NPC::getCharacterBackground() {
	return characterBackground;
}

float NPC::getOriginalLengthX() {
	return len_x;
}

float NPC::getOriginalLengthY() {
	return len_y;
}

float NPC::getOriginalLengthZ() {
	return len_z;
}

float NPC::getOriginalTranslateX() {
	return t_x;
}

float NPC::getOriginalTranslateY() {
	return t_y;
}

float NPC::getOriginalTranslateZ() {
	return t_z;
}

float NPC::getOriginalScaleX() {
	return s_x;
}

float NPC::getOriginalScaleY() {
	return s_y;
}

float NPC::getOriginalScaleZ() {
	return s_z;
}

float NPC::getRadius() {
	return radius;
}

Float3 NPC::getCenterPoint() {
	return centerPoint;
}

void NPC::modifyRadius(APoint scale_p) {
	float new_len_x = len_x * scale_p[0];
	float new_len_z = len_z * scale_p[2];
	
	float half_len_x = new_len_x / 2;
	float half_len_z = new_len_z / 2;
	radius = half_len_x > half_len_z ? half_len_x :half_len_z;
}

void NPC::modifyCenterPoint(APoint translate_p) {
	centerPoint[0] += translate_p[0];
	centerPoint[1] += translate_p[1];
	centerPoint[2] += translate_p[2];
}

void NPC::printData() {
	std::cout << "ID: " << characterID << "\n";
	std::cout << "Title: " << characterTitle << "\n";
	std::cout << "Name: " << characterName << "\n";
	std::cout << "Species: " << characterSpecies << "\n";
	std::cout << "Background: " << characterBackground << "\n";
	std::cout << "ObjFilename: " << objFilename << "\n";
	std::cout << "DialogFilename: " << dialogFilename << "\n";
	std::cout << "DialogTree:\n";
	std::cout << "Translation: " << t_x << ", " << t_y << ", " << t_z << "\n";

	dialogTree->printData();
}